import java.util.Calendar;
import java.util.Date;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class CalculateTimeUsed {

	double[][][] cube;
	int[] routes;
	Calendar day = Calendar.getInstance();
	GenCubeMatrix gen;

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public CalculateTimeUsed(double[][][] cube, int[] routes, GenCubeMatrix gen) throws IOException {
		this.cube = cube;
		this.routes = routes;
		this.gen = gen;
		measureTimeUsed();
	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public void measureTimeUsed() throws IOException {
		double total_TimeUsed = 0;
		String dayOfWeek = getToday(day.get(Calendar.YEAR),
				day.get(Calendar.MONTH), 25);

		String timeClass = getCurrentTimeClass(day.getTime());

		int indexOfday = gen.getDaysTable().indexOf(dayOfWeek.toLowerCase());
		int indexOfTime = gen.getTimesTable().indexOf(timeClass);

		for (int i = 0; i < routes.length; i++) {
			int indexOfLink = gen.getLinkIDArrUnique().indexOf("" + routes[i]);
			if (indexOfLink > -1) {
				// System.out.println(indexOfday + " - " + indexOfTime + " - "
				// + indexOfLink + " = " + routes[i]);
				// System.out.println(dayOfWeek + " - " + timeClass + " - "
				// + routes[i]);
				total_TimeUsed = total_TimeUsed
						+ cube[indexOfday][indexOfTime][indexOfLink];

			} else {
				// Specific a constant in case of link not found from map
				// matching algorithm
				total_TimeUsed = total_TimeUsed + 25;

			}
		}
		Util.writeLog("4.3 Calculate the time used for travelling");
		System.out.println("total_TimeUsed : " + total_TimeUsed / 60);
		
	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public String getToday(int year, int month, int date) {

		day.set(Calendar.YEAR, year);
		day.set(Calendar.MONTH, month);
		day.set(Calendar.DAY_OF_MONTH, date);

		String dayOfWeek = Util.currentDaysOfWeek(day);
		// System.out.print(dayOfWeek);
		return dayOfWeek;
	}

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public String getCurrentTimeClass(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String timeclass = Util.setTimeClass(sdf.format(date));
		// System.out.print(timeclass);
		return timeclass;

	}
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
